/**
 * 
 */
package sk.lukaskrupa.guess.the.color.ui;

import sk.lukaskrupa.guess.the.color.listeners.ColorPickerButtonListener;
import sk.lukaskrupa.guess.the.color.logging.Logging;
import android.app.Dialog;
import android.view.View;
import android.widget.ImageButton;

/**
 * Shows pop up dialog with colors.
 * 
 * @author Lukas Krupa (lukas0krupa@gmail.com)
 * 
 */
public class ColorPicker {

	private static Logging logger = Logging.getLog(ColorPicker.class);

	private final Dialog dialog;

	private final ColorPickerButtonListener colorPickerButtonListener;

	/**
	 * Contructs Color picker button listener.
	 * 
	 * @param game
	 *            to save reference to
	 * @param colorPickerButtonListener
	 *            to handle clicks
	 * @param i
	 */
	public ColorPicker(Game game, ColorPickerButtonListener colorPickerButtonListener) {
		this.colorPickerButtonListener = colorPickerButtonListener;

		logger.debug("ColorPicker", "Creting dialog.");
		this.dialog = new Dialog(game);
		this.dialog.setContentView(R.layout.colorpicker);
		this.dialog.setTitle(R.string.popup_title);

		logger.debug("ColorPicker", "Setting listeners for popup window buttons.");
		this.dialog.findViewById(R.id.popup_button1).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button2).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button3).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button4).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button5).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button6).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button7).setOnClickListener(colorPickerButtonListener);
		this.dialog.findViewById(R.id.popup_button8).setOnClickListener(colorPickerButtonListener);

	}
	/**
	 * Showing color picker.
	 * 
	 * @param invokedButton
	 *            button which invoked it.
	 */
	public void showColorPicker(ImageButton invokedButton) {
		logger.info("showColorPicker", "Showing popup window.");
		this.colorPickerButtonListener.setInvokedOn(invokedButton);
		this.dialog.show();
	}

	/**
	 * Closing color picker.
	 */
	public void closeColorPicker() {
		logger.info("closewColorPicker", "Closing popup window.");
		this.dialog.hide();
	}
	/**
	 * Show given button. (button ID).
	 * 
	 * @param buttonId
	 *            ID to show
	 */
	public void showButton(int buttonId) {
		this.dialog.findViewById(buttonId).setVisibility(View.VISIBLE);
	}

	/**
	 * Hide given button. (button ID).
	 * 
	 * @param buttonId
	 *            ID to show
	 */
	public void hideButton(int buttonId) {
		this.dialog.findViewById(buttonId).setVisibility(View.GONE);
	}

	/**
	 * Enables all buttons.
	 */
	public void enableButtons() {

		((ImageButton) this.dialog.findViewById(R.id.popup_button1)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.black));
		((ImageButton) this.dialog.findViewById(R.id.popup_button1)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button6)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.blue));
		((ImageButton) this.dialog.findViewById(R.id.popup_button6)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button3)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.green));
		((ImageButton) this.dialog.findViewById(R.id.popup_button3)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button2)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.red));
		((ImageButton) this.dialog.findViewById(R.id.popup_button2)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button4)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.white));
		((ImageButton) this.dialog.findViewById(R.id.popup_button4)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button5)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.yellow));
		((ImageButton) this.dialog.findViewById(R.id.popup_button5)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button7)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.brown));
		((ImageButton) this.dialog.findViewById(R.id.popup_button7)).setClickable(true);

		((ImageButton) this.dialog.findViewById(R.id.popup_button8)).setImageDrawable(this.dialog.getContext().getResources()
				.getDrawable(R.drawable.purple));
		((ImageButton) this.dialog.findViewById(R.id.popup_button8)).setClickable(true);
	}
}
